﻿// http://stackoverflow.com/questions/490177/how-do-i-determine-elegantly-if-proxy-authentication-is-required-in-c-sharp-wi


using System;
using System.Net;
using System.IO;
//using FernandoSystem.Net;

// http://referencesource.microsoft.com/netframework.aspx
// C:\Documents and Settings\Fernando\Local Settings\Application Data\RefSrcSymbols\

namespace HttpRequest
{

    class DiscoVirtual
    {
        public void Step1()
        {
            string nextUrl = "http://www.google.com.ar";

            Uri uri = new Uri(nextUrl);

            if (uri.Scheme == Uri.UriSchemeHttp)
            {
                //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(nextUrl);
                //HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = WebRequestMethods.Http.Get;


                //---- Proxies ---------------------------------------------------------------------------------------------

                //IWebProxy proxy = request.Proxy;
                //if (proxy != null)
                //{
                //    Console.WriteLine("Proxy: {0}", proxy.GetProxy(request.RequestUri));
                //}
                //else
                //{
                //    Console.WriteLine("Proxy is null; no proxy will be used");
                //}

                //WebProxy proxyObject = new WebProxy("http://proxyserver:80/", true);
                //Console.WriteLine(proxyObject.UseDefaultCredentials);
                //request.Proxy = proxyObject;

                //IWebProxy proxy2 = WebRequest.GetSystemWebProxy();
                var proxy2 = new WebProxy("127.0.0.1", 9090);
                proxy2.Credentials = CredentialCache.DefaultCredentials;
                request.Proxy = proxy2;


                //proxy2.Credentials = new NetworkCredential(appProxySettings.proxyUsername, appProxySettings.proxyPassword)
                //WebProxy proxyObj = new WebProxy("10.10.249.19", 8080) ;
                //NetworkCredential networkCredential = new NetworkCredential("yourname", "yourpass") ; 

                //---- Proxies ---------------------------------------------------------------------------------------------









                //request.KeepAlive = true;
                //request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                //request.Headers.Add("Accept-Encoding", "gzip, deflate");
                //request.Headers.Add("Accept-Language", "es-ar");
                //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; FDM; .NET4.0C; .NET4.0E)";
                //request.Referer = "https://www3.discovirtual.com.ar/login/prehome.aspx";
                ////request.Host = "www3.discovirtual.com.ar";
                ////request.Headers.Add("Host", "www3.discovirtual.com.ar");
                //request.AllowAutoRedirect = false;


                try
                {
                    string tmp;

                    //using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
                    {
                        //nextUrl = GetRedirectURL(response, ref _cookie);

                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        tmp = reader.ReadToEnd();
                    }

                    Console.Write(tmp);

                }
                catch (WebException wex)
                {
                    HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
                    if (httpResponse.StatusCode == HttpStatusCode.NotModified)
                    {
                        // resource was not modified.
                    }

                    // Something else happened. Rethrow or log.
                    throw;
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DiscoVirtual dv = new DiscoVirtual();
            dv.Step1();
        }
    }
}




//--------------------------------------------------------------------------------------------

//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Net;
//using System.IO;



//namespace HttpRequest
//{

//    class DiscoVirtual
//    {
//        private Cookie _cookie = null;
//        string _nextUrl = "https://www3.discovirtual.com.ar/login/Invitado.aspx";

//        //public static string GetRedirectURL(HttpWebResponse webresponse, ref string Cookie)
//        public static string GetRedirectURL(HttpWebResponse webresponse, ref Cookie cookie)
//        {
//            string uri = "";

//            WebHeaderCollection headers = webresponse.Headers;

//            if ((webresponse.StatusCode == HttpStatusCode.Found) ||
//                (webresponse.StatusCode == HttpStatusCode.Redirect) ||
//                (webresponse.StatusCode == HttpStatusCode.Moved) ||
//                (webresponse.StatusCode == HttpStatusCode.MovedPermanently))
//            {
//                uri = headers["Location"];
//                uri = uri.Trim();
//            }

//            //Check for any cookies

//            //var tmp = webresponse.Cookies[0];   //["Set-Cookie"];
//            cookie = webresponse.Cookies[0];   //["Set-Cookie"];

//            if (headers["Set-Cookie"] != null)
//            {
//                //Cookie = headers["Set-Cookie"];
//                //cookie = headers["Set-Cookie"];
//            }

//            return uri;
//        }


//        //GET /Comprar/Menu.aspx?IdLocal=9235&IdTipoCompra=4&Fecha=20110817 HTTP/1.1

//        public void Step1()
//        {
//            //string _nextUrl = "https://www3.discovirtual.com.ar/login/Invitado.aspx";

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_nextUrl);
//            request.CookieContainer = new CookieContainer();
//            //request.CookieContainer.Add()

//            request.Method = "GET";
//            request.KeepAlive = true;

//            request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
//            request.Headers.Add("Accept-Encoding", "gzip, deflate");
//            request.Headers.Add("Accept-Language", "es-ar");
//            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; FDM; .NET4.0C; .NET4.0E)";

//            request.Referer = "https://www3.discovirtual.com.ar/login/prehome.aspx";
//            //request.Host = "www3.discovirtual.com.ar";
//            //request.Headers.Add("Host", "www3.discovirtual.com.ar");

//            request.AllowAutoRedirect = false;

//            try
//            {

//                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
//                {
//                    //Cookie cookie = null;
//                    //string newUri = GetRedirectURL(response, ref _cookie);
//                    _nextUrl = GetRedirectURL(response, ref _cookie);
                    

//                    //foreach (Cookie cook in response.Cookies)
//                    //{
//                    //    Console.WriteLine("Cookie:");
//                    //    Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
//                    //    Console.WriteLine("Domain: {0}", cook.Domain);
//                    //    Console.WriteLine("Path: {0}", cook.Path);
//                    //    Console.WriteLine("Port: {0}", cook.Port);
//                    //    Console.WriteLine("Secure: {0}", cook.Secure);

//                    //    Console.WriteLine("When issued: {0}", cook.TimeStamp);
//                    //    Console.WriteLine("Expires: {0} (expired? {1})",
//                    //        cook.Expires, cook.Expired);
//                    //    Console.WriteLine("Don't save: {0}", cook.Discard);
//                    //    Console.WriteLine("Comment: {0}", cook.Comment);
//                    //    Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
//                    //    Console.WriteLine("Version: RFC {0}", cook.Version == 1 ? "2109" : "2965");

//                    //    // Show the string representation of the cookie.
//                    //    Console.WriteLine("String: {0}", cook.ToString());
//                    //}

//                    //var responseStream = response.GetResponseStream();
//                    //StreamReader reader = new StreamReader(responseStream);
//                    //string s = reader.ReadToEnd();
//                    //Console.WriteLine(s);
//                }
//            }
//            catch (WebException wex)
//            {
//                HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
//                if (httpResponse.StatusCode == HttpStatusCode.NotModified)
//                {
//                    // resource was not modified.
//                }

//                // Something else happened. Rethrow or log.
//                throw;
//            }

        
//        }
//        public void Step2()
//        {
//            _nextUrl = "https://www3.discovirtual.com.ar/Comprar/Menu.aspx?IdLocal=9235&IdTipoCompra=4&Fecha=20110817"; //TODO:

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_nextUrl);
//            request.CookieContainer = new CookieContainer();
//            request.CookieContainer.Add(_cookie);

//            request.Method = "GET";
//            request.KeepAlive = true;

//            request.Accept = "*/*";
//            request.Headers.Add("Accept-Encoding", "gzip, deflate");
//            request.Headers.Add("Accept-Language", "es-ar");
//            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; FDM; .NET4.0C; .NET4.0E)";

//            request.Referer = "https://www3.discovirtual.com.ar/Comprar/Home.aspx";
//            //request.Host = "www3.discovirtual.com.ar";
//            //request.Headers.Add("Host", "www3.discovirtual.com.ar");

//            request.AllowAutoRedirect = false;


//            try
//            {
//                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
//                {
//                    var responseStream = response.GetResponseStream();
//                    StreamReader reader = new StreamReader(responseStream);
//                    string s = reader.ReadToEnd();
//                    Console.WriteLine(s);
//                }
//            }
//            catch (WebException wex)
//            {
//                HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
//                if (httpResponse.StatusCode == HttpStatusCode.NotModified)
//                {
//                    // resource was not modified.
//                }

//                // Something else happened. Rethrow or log.
//                throw;
//            }


//        }

//        public void Step3()
//        {
//            _nextUrl = "https://www3.discovirtual.com.ar/ajaxpro/_MasterPages_Home,DiviComprasWeb.ashx?method=MostrarGondola";

//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_nextUrl);
//            request.CookieContainer = new CookieContainer();
//            request.CookieContainer.Add(_cookie);


//            request.Method = "POST";
//            request.KeepAlive = true;
//            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; FDM; .NET4.0C; .NET4.0E)";
//            request.Accept = "*/*";
//            request.Headers.Add("Accept-Encoding", "gzip, deflate");
//            request.Headers.Add("Accept-Language", "es-ar");
//            request.Referer = "https://www3.discovirtual.com.ar/Comprar/Home.aspx";
//            //request.Host = "www3.discovirtual.com.ar";
//            //request.Headers.Add("Host", "www3.discovirtual.com.ar");

//            request.AllowAutoRedirect = false;
//            request.Headers.Add("x-ajaxpro-method", "MostrarGondola");



//            string postData = "{\"idMenu\":3275}";
//            ASCIIEncoding encoding = new ASCIIEncoding();
//            byte[] byte1 = encoding.GetBytes(postData);

//            request.ContentType = "application/x-www-form-urlencoded";
//            request.ContentLength = byte1.Length;   //15;

//            Stream newStream = request.GetRequestStream();
//            newStream.Write(byte1, 0, byte1.Length);


//            //----------------------------------------------------------------------------------------------------------------



//            try
//            {
//                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
//                {
//                    var responseStream = response.GetResponseStream();
//                    StreamReader reader = new StreamReader(responseStream);
//                    string s = reader.ReadToEnd();
//                    Console.WriteLine(s);

//                    //int bufferSize = 1024;
//                    //byte[] buffer = new byte[bufferSize];
//                    //int bytesRead;


//                    //while ((bytesRead = responseStream.Read(buffer, 0, bufferSize)) > 0)
//                    //{
//                    //    //Response.OutputStream.Write(buffer, 0, bytesRead);
//                    //}
//                }
//            }
//            catch (WebException wex)
//            {
//                HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
//                if (httpResponse.StatusCode == HttpStatusCode.NotModified)
//                {
//                    // resource was not modified.
//                }

//                // Something else happened. Rethrow or log.
//                throw;
//            }


//        }



//        public void StepZ()
//        {

//            //string url = "http://www.google.com.ar";
//            //string url = "https://www3.discovirtual.com.ar/ajaxpro/_MasterPages_Home,DiviComprasWeb.ashx?method=MostrarGondola";

//            string url = "https://www3.discovirtual.com.ar/login/Invitado.aspx";
//            //string url = "https://www3.discovirtual.com.ar/Comprar/Home.aspx";


//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//            request.CookieContainer = new CookieContainer();
//            //request.CookieContainer.Add()


//            request.Method = "GET";
//            request.KeepAlive = true;

//            request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
//            //request.Accept = "*/*";
//            request.Headers.Add("Accept-Encoding", "gzip, deflate");
//            request.Headers.Add("Accept-Language", "es-ar");
//            //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2)";
//            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; FDM; .NET4.0C; .NET4.0E)";

//            request.Referer = "https://www3.discovirtual.com.ar/login/prehome.aspx";
//            //request.Host = "www3.discovirtual.com.ar";
//            //request.Headers.Add("Host", "www3.discovirtual.com.ar");

//            request.AllowAutoRedirect = false;

//            //request.Headers.Add("x-ajaxpro-method", "MostrarGondola");
//            //request.Referer = "https://www3.discovirtual.com.ar/Comprar/Home.aspx";



//            //string postData = "{\"idMenu\":3275}";
//            //ASCIIEncoding encoding = new ASCIIEncoding();
//            //byte[] byte1 = encoding.GetBytes(postData);

//            //request.ContentType = "application/x-www-form-urlencoded";
//            //request.ContentLength = byte1.Length;   //15;

//            //Stream newStream = request.GetRequestStream();
//            //newStream.Write(byte1, 0, byte1.Length);


//            //----------------------------------------------------------------------------------------------------------------



//            try
//            {

//                //using (WebResponse response = request.GetResponse())
//                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
//                {

//                    //string cookie = "";
//                    Cookie cookie = null;
//                    string newUri = GetRedirectURL(response, ref cookie);

//                    // Print the properties of each cookie.
//                    foreach (Cookie cook in response.Cookies)
//                    {
//                        Console.WriteLine("Cookie:");
//                        Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
//                        Console.WriteLine("Domain: {0}", cook.Domain);
//                        Console.WriteLine("Path: {0}", cook.Path);
//                        Console.WriteLine("Port: {0}", cook.Port);
//                        Console.WriteLine("Secure: {0}", cook.Secure);

//                        Console.WriteLine("When issued: {0}", cook.TimeStamp);
//                        Console.WriteLine("Expires: {0} (expired? {1})",
//                            cook.Expires, cook.Expired);
//                        Console.WriteLine("Don't save: {0}", cook.Discard);
//                        Console.WriteLine("Comment: {0}", cook.Comment);
//                        Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
//                        Console.WriteLine("Version: RFC {0}", cook.Version == 1 ? "2109" : "2965");

//                        // Show the string representation of the cookie.
//                        Console.WriteLine("String: {0}", cook.ToString());
//                    }

//                    var responseStream = response.GetResponseStream();
//                    StreamReader reader = new StreamReader(responseStream);
//                    string s = reader.ReadToEnd();
//                    Console.WriteLine(s);

//                    //int bufferSize = 1024;
//                    //byte[] buffer = new byte[bufferSize];
//                    //int bytesRead;


//                    //while ((bytesRead = responseStream.Read(buffer, 0, bufferSize)) > 0)
//                    //{
//                    //    //Response.OutputStream.Write(buffer, 0, bytesRead);
//                    //}
//                }
//            }
//            catch (WebException wex)
//            {
//                HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
//                if (httpResponse.StatusCode == HttpStatusCode.NotModified)
//                {
//                    // resource was not modified.
//                }

//                // Something else happened. Rethrow or log.
//                throw;
//            }


//        }
//    }

//    class Program
//    {
//        static void Main(string[] args)
//        {
//            DiscoVirtual dv = new DiscoVirtual();
//            dv.Step1();
//            dv.Step2();
//            dv.Step3();
//        }
//    }
//}

